package com.ankiang.treeprint.fragment.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.ankiang.treeprint.R;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import org.intellij.lang.annotations.RegExp;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class TRUtils {

    /**
     * 添加一个功能项
     *
     * @param section
     * @param title
     * @param listener
     */
    public static void addFunctionItem(QMUIGroupListView.Section section, QMUIGroupListView groupListView,
                                       String title, int rid, View.OnClickListener listener) {
        QMUICommonListItemView itemWithChevron = groupListView.createItemView(title);
        itemWithChevron.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        itemWithChevron.setImageDrawable(groupListView.getContext().getDrawable(rid));
        section.addItemView(itemWithChevron, listener);
    }


    /**
     *  用分隔符将字符串列表连接在一起
     * @param delim 分隔符
     * @param list 字符串列表
     * @return
     */
    public static String myJoin(String delim, List<String> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i != 0) {
                sb.append(delim);
            }
            String v = list.get(i);
            sb.append(v);
        }
        return sb.toString();
    }


    /**
     * 缩小图片
     */
    static Bitmap drawableToBitmap(Drawable drawable) {// drawable 转换成 bitmap

        int width = drawable.getIntrinsicWidth();  // 取 drawable 的长宽
        int height = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;     // 取 drawable 的颜色格式
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);   // 建立对应 bitmap
        Canvas canvas = new Canvas(bitmap);     // 建立对应 bitmap 的画布
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);    // 把 drawable 内容画到画布中
        return bitmap;
    }

    public static boolean isRightTimeFormat(String s){
        String regex = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";
        Pattern p = Pattern.compile(regex);
        return p.matcher(s).find();
    }


    public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable); // drawable 转换成 bitmap
        Matrix matrix = new Matrix();  // 创建操作图片用的 Matrix 对象
        float scaleWidth = ((float) w / width);  // 计算缩放比例
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);     // 设置缩放比例
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);    // 建立新的 bitmap ，其内容是对原 bitmap 的缩放后的图
        return new BitmapDrawable(newbmp); // 把 bitmap 转换成 drawable 并返回
    }



    /**
     * 用于修复scrollView包含listview导致只显示一条的问题
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();

        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() + 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

}
